angular.module('wineservices', [])
  //酒款Service
  .factory('Wine',function($resource,$rootScope,ENV){

    //访问的地址
    var APIUrl = ENV.api;
    //保存返回的数据
    var allWineListJson,commentFWineListJson,collectFWineListJson;
    var pageSize = 5;
    var mmWineListJson,wineListCount;//约局的搜索酒款
    var wineDetailJson,wineDetailSearchJson,addWineRecommendJson,collectWineJson;
    var collectWineListJson,wineListJson,wineListByDetailJson;
    var wineListResource = $resource(APIUrl+"product/getWineList.do");
    var wineDetailResource = $resource(APIUrl+"product/getWineDetailById.do");
    var wineSearchResource = $resource(APIUrl+"productSearch/getSearchWineList.do");
    var wineDetailSearchResource = $resource(APIUrl+"product/getDetailSearch.do");
    var WineListByDetailResource = $resource(APIUrl+"product/getwineListByds.do");
    var addToWineRecommendResource = $resource(APIUrl+"product/addToWineRecommend.do");
    var collectWineResource = $resource(APIUrl+"product/addWineCollect.do");
    //获取到我收藏的酒款--->发布约局时，查看我收藏的酒款
    var getCollectWineListRescource = $resource(APIUrl+"releaseMeeting/getCollectWineList.do");
    var getScanSearchResource = $resource(APIUrl+"productScan/getScanSearch.do");
    //var getMmWineListByKeyResource = $resource(APIUrl+"productSearch/getMmWineListByKey.do");
    return{
      //获取约局酒款
      getMmWineListByKey:function(wineKey,orderBy,order){
        var hasNextPage = true;
        wineSearchResource.save({
          wine_name:wineKey,
          pageIndex:1,
          pageSize:pageSize,
          orderBy:orderBy,
          order:order
        },function(response){
          if(response.wineList.length<pageSize){
            hasNextPage = false;
          }
          mmWineListJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.wineList
          };
          wineListCount = response.wineListCount;
          $rootScope.$broadcast('Wine.MmWineListByKeyUpdated');
        });
      },
      getMoreMmWineListByKey:function(wineKey,orderBy,order){
        if(mmWineListJson===undefined){
          return false;
        }
        var hasNextPage = mmWineListJson.hasNextPage;
        var nextPage = mmWineListJson.nextPage;
        if(!hasNextPage){
          return false;
        }
        wineSearchResource.save({
          wine_name:wineKey,
          pageIndex:nextPage,
          pageSize:pageSize,
          orderBy:orderBy,
          order:order
        },function(response){
          nextPage++;
          if(response.wineList.length<pageSize){
            hasNextPage = false;
          }
          mmWineListJson = {
            hasNextPage:hasNextPage,
            nextPage:nextPage,
            data:response.wineList
          };
          console.log(response.wineList.length);
          $rootScope.$broadcast('Wine.MoreMmWineListByKeyUpdated');
        });
      },
      getMmWineListJson:function(){
        return mmWineListJson.data;
      },
      //获取搜索到的酒款的数量
      getMmWineListCount:function(){
        return wineListCount;
      },
      mmWineHasNextPage:function(){
        if(mmWineListJson===undefined){
          return false;
        }
        return mmWineListJson.hasNextPage;
      },
      //获取酒款的列表
      getWineList:function(wineOrder){
        var hasNextPage = true;
        wineListResource.save({
          wine_order:wineOrder,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(wineOrder=='wine_comment_num'){
            if(response.wineList.length<pageSize){
              hasNextPage = false;
            }
            commentFWineListJson = {
              hasNextPage:hasNextPage,
              nextPage:2,
              data:response.wineList
            };
            $rootScope.$broadcast('Wine.CommentWineListUpdated');//评论优先
          }else if(wineOrder=='wine_collect_num'){
            if(response.wineList.length<pageSize){
              hasNextPage = false;
            }
            collectFWineListJson = {
              hasNextPage:hasNextPage,
              nextPage:2,
              data:response.wineList
            };
            $rootScope.$broadcast('Wine.CollectWineListUpdated');//收藏优先
          }else{
            if(response.wineList.length<pageSize){
              hasNextPage = false;
            }
            allWineListJson = {
              hasNextPage:hasNextPage,
              nextPage:2,
              data:response.wineList
            };
            $rootScope.$broadcast('Wine.WineListUpdated');//全部酒款
          }
        });
      },
      //加载更多的全部酒款
      getMoreAllWineList:function(){
        if(allWineListJson===undefined){
          return false;
        }
        var hasNextPage=allWineListJson.hasNextPage;
        var nextPage=allWineListJson.nextPage;
        //var moreWineData=allWineListJson.data;
        if(!hasNextPage){
          return false;
        }
        wineListResource.save({
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
            nextPage++;
            if (response.wineList.length < pageSize) {
              hasNextPage = false;
            }
          //moreWineData = moreWineData.concat(response.wineList);
          allWineListJson = {
              hasNextPage: hasNextPage,
              nextPage: nextPage,
              data: response.wineList
            };
            $rootScope.$broadcast('Wine.MoreWineListUpdated');//全部酒款

          });
      },
      //加载更多的评论优先
      getMoreCommentFWineList:function(){
      if(commentFWineListJson===undefined){
        return false;
      }
      var hasNextPage=commentFWineListJson.hasNextPage;
      var nextPage=commentFWineListJson.nextPage;
      //var moreWineData=commentFWineListJson.data;
      if(!hasNextPage){
        return false;
      }
      wineListResource.save({
        wine_order:'wine_comment_num',
        pageIndex:nextPage,
        pageSize:pageSize
      },function(response){
        nextPage++;
        if (response.wineList.length < pageSize) {
          hasNextPage = false;
        }
        //moreWineData = moreWineData.concat(response.wineList);
        commentFWineListJson = {
          hasNextPage: hasNextPage,
          nextPage: nextPage,
          data: response.wineList
        };
        $rootScope.$broadcast('Wine.MoreCommentWineListUpdated');//评论优先

      });
    },
      //加载更多的收藏优先
      getMoreCollectFWineList:function(){
        if(collectFWineListJson===undefined){
          return false;
        }
        var hasNextPage=collectFWineListJson.hasNextPage;
        var nextPage=collectFWineListJson.nextPage;
       // var moreWineData=collectFWineListJson.data;
        if(!hasNextPage){
          return false;
        }
        wineListResource.save({
          wine_order:'wine_collect_num',
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if (response.wineList.length < pageSize) {
            hasNextPage = false;
          }
         // moreWineData = moreWineData.concat(response.wineList);
          collectFWineListJson = {
            hasNextPage: hasNextPage,
            nextPage: nextPage,
            data: response.wineList
          };
          $rootScope.$broadcast('Wine.MoreCollectWineListUpdated');//收藏优先
        });
      },
      //全部酒款是否有下一页
      allWineHasNextPage: function() {
        if (allWineListJson === undefined) {
          return false;
        }
        return allWineListJson.hasNextPage;
      },
      //评论优先酒款是否有下一页
      commentFHasNextPage: function() {
        if (commentFWineListJson === undefined) {
          return false;
        }
        return commentFWineListJson.hasNextPage;
      },
      //收藏优先酒款是否有下一页
      collectFHasNextPage: function() {
        if (collectFWineListJson === undefined) {
          return false;
        }
        return collectFWineListJson.hasNextPage;
      },
      //获取所有的酒款
      getAllWineListJson:function(){
        return allWineListJson.data;
      },
      //获取评论优先的酒款
      getCommentFWineListJson:function(){
        return commentFWineListJson.data;
      },
      //获取收藏优先的全部酒款
      getCollectFWineListJson:function(){
        return collectFWineListJson.data;
      },
      //根据条形码获取酒款信息
      getWineByBarCode:function(barCode){
        getScanSearchResource.save({
          wine_barcode:barCode
        },function(response){
          wineListJson = response;
          $rootScope.$broadcast('Wine.WineSearchUpdated');
        });
      },
      //获取到我收藏酒款的列表
      getCollectWineList:function(memberId){
        getCollectWineListRescource.save({
          member_id:memberId
        },function(response){
          collectWineListJson = response;
          $rootScope.$broadcast('Wine.getCollectWineListUpdated');//收藏优先
        });
      },
      getCollectWineListJson:function(){
          return collectWineListJson;
      },
      //精细查找
      getWineListByDetail:function(countryIds,typeIds,occasionIds,priceId,orderBy,order){
        var hasNextPage = true;
        WineListByDetailResource.save({
          searchcountry:countryIds,
          searchWineType:typeIds,
          searchSituation:occasionIds,
          searchPrice:priceId,
          pageIndex:1,
          pageSize:pageSize,
          orderBy:orderBy,
          order:order
        },function(response){
          if(response.wineList.length<pageSize){
            hasNextPage = false;
          }
          wineListByDetailJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.wineList
          };
          wineListCount = response.wineListCount;
          $rootScope.$broadcast('Wine.WineSearchByDetailUpdated');//精细查找
        });
      },
      //加载更多精细查找到的酒款
      getMoreWineListByDetail:function(countryIds,typeIds,occasionIds,priceId,orderBy,order){
        if(wineListByDetailJson===undefined){
          return false;
        }
        var hasNextPage=wineListByDetailJson.hasNextPage;
        var nextPage=wineListByDetailJson.nextPage;
        // var moreWineData=collectFWineListJson.data;
        if(!hasNextPage){
          return false;
        }
        WineListByDetailResource.save({
          searchcountry:countryIds,
          searchWineType:typeIds,
          searchSituation:occasionIds,
          searchPrice:priceId,
          pageIndex:nextPage,
          pageSize:pageSize,
          orderBy:orderBy,
          order:order
        },function(response){
          nextPage++;
          if (response.wineList.length < pageSize) {
            hasNextPage = false;
          }
          // moreWineData = moreWineData.concat(response.wineList);
          wineListByDetailJson = {
            hasNextPage: hasNextPage,
            nextPage: nextPage,
            data: response.wineList
          };
         // wineListCount = response.wineListCount;
          $rootScope.$broadcast('Wine.MoreWineSearchByDetailUpdated');//获取更多加载更多的
        });
      },
      //精细查找是否有下一页
      detailSearchHasNextPage:function(){
        if (wineListByDetailJson === undefined) {
          return false;
        }
        return wineListByDetailJson.hasNextPage;
      },
      //获取精细查找搜索到的酒款列表
      getWineListByDetailJson:function(){
        return wineListByDetailJson.data;
      },
      //加入推荐酒单
     add2WineRecommend:function(recommendInfo){
       addToWineRecommendResource.save({
         newWines:recommendInfo
       },function(response){
         addWineRecommendJson = response;
         $rootScope.$broadcast('Wine.add2WineRecommendUpdated');
       });
     },
     /* //根据酒款的名称获取到酒款的列表
      getSearchWineList:function(wineName){
        wineSearchResource.save({
                wine_name:wineName
              },function(response){
                wineListJson = response;
                $rootScope.$broadcast('Wine.WineSearchUpdated');
              });
      },*/
      //获取酒款的详情
      getWineDetailById:function(memberId,wineId){
        wineDetailResource.save({
          member_id:memberId,
          wine_id:wineId
        },function(response){
          wineDetailJson = response;
          $rootScope.$broadcast('Wine.WineDetailUpdated');
        });
      },
      //获取加入推荐酒单返回值
      getAddWineRecommendJson:function(){
        return addWineRecommendJson;
      },
      //获取搜索的信息，如国家，类型，饮用场合
      getSearchDetail:function(){
        wineDetailSearchResource.save({
          member_id:1
        },function(response){
          wineDetailSearchJson = response;
          $rootScope.$broadcast('Wine.WineSearchDetailUpdated');
        });
      },
      //返回酒款列表的Json数据
      getWineListJson:function(){
        return wineListJson;
      },
      //获取酒款详情的Json数据
      getWineDetailJson:function(){
        return wineDetailJson;
      },
      //获取到搜索的信息的Json
      getSearchDetailJson:function(){
        return wineDetailSearchJson;
      },
      //收藏该酒款
      collectWine:function(wineId,memberId){
        collectWineResource.save({
          member_id:memberId,
          mc_obj_id:wineId
        },function(response){
          collectWineJson = response;
          $rootScope.$broadcast('Wine.collectBWineUpdated');
        });
      },
      //获取收藏的信息
      getCollectWineJson:function(){
        return collectWineJson;
      }
    }
  })







//评论的Service
  .factory('WineComment', function($resource,$rootScope,ENV) {
    var APIUrl = ENV.api;

    var commentListJson,pageSize = 10,
      memberInfo,wineCommentJson,commentCount;

    var wineCommentResource = $resource(APIUrl+"product/getWineCommentList.do");
    var publishCommentResource = $resource(APIUrl+"product/addWineComment.do");
    var getCommentCountResource = $resource(APIUrl+"product/getCommentCount.do");
    var deleteCommentResource = $resource(APIUrl+"product/delWineComment.do");

    //获取的约局的评论列表
    return {
      //删除评论
      deleteComment:function(commentId,memberId,wineId){
        deleteCommentResource.save({
          wine_comment_id:commentId,
          member_id:memberId,
          wine_id:wineId
        },function(response){
          deleteCommentJson = response;
          $rootScope.$broadcast('WineComment.deleteCommentUpdated');
        });
      },
      getDelCommentJson:function(){
        return deleteCommentJson;
      },
      getCommentList:function(wineId,memberId){
        var hasNextPage = true;
        wineCommentResource.save({
          wine_id:wineId,
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.commentList.length<pageSize){
            hasNextPage = false;
          }
          memberInfo = response.memberInfo;
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage: 2,
            data: response.commentList
          };
          $rootScope.$broadcast('Wine.CommentListUpdated');
        })
      },
      getMoreCommentList:function(wineId,memberId){
        if(commentListJson===undefined){
          return false;
        }
        var hasNextPage=commentListJson.hasNextPage;
        var nextPage=commentListJson.nextPage;
        var moreCommentData=commentListJson.data;
        if(!hasNextPage){
          return false;
        }
        wineCommentResource.save({
          wine_id:wineId,
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.commentList.length<pageSize){
            hasNextPage = false;
          }
          moreCommentData = moreCommentData.concat(response.commentList);
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreCommentData
          };
          $rootScope.$broadcast('Wine.MoreCommentListUpdated');
        });
      },
      //是否有下一页
      hasNextPage: function() {
        if (commentListJson === undefined) {
          return false;
        }
        return commentListJson.hasNextPage;
      },
      getMemberInfo:function(){
        return memberInfo;
      },
      //发表评论
      publishComment:function(wineId,memberId,newComment){
        publishCommentResource.save({
          member_id:memberId,
          wine_id:wineId,
          wine_comment_content:newComment
        },function(response){
          wineCommentJson = response;
          $rootScope.$broadcast('Wine.publishCommentUpdated');
        });
      },
      //获取评论的数量
      getCommentCount:function(wineId,memberId){
        getCommentCountResource.save({
          wine_id:wineId,
          member_id:memberId
        },function(response){
          commentCount = response;
          $rootScope.$broadcast('Wine.commentCountUpdated');
        });
      },
      getCommentCountJson:function(){
        return commentCount;
      },
      getCommentListJson:function(){
        return commentListJson.data;
      },
      //获取到新增的评论内容
      getNewComment:function(){
        return wineCommentJson;
      }
    }
  })

;
